#include "verifyextradialog.h"
#include "ui_verifyextradialog.h"
#include "utils.h"

#include <QSqlQuery>

VerifyExtraDialog::VerifyExtraDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::VerifyExtraDialog)
{
    ui->setupUi(this);
    init();
}

VerifyExtraDialog::~VerifyExtraDialog()
{
    delete ui;
}

void VerifyExtraDialog::setFSub(int fid)
{
    this->fid = fid;
    int idx = ui->cmbSubs->findData(fid);
    ui->cmbSubs->setCurrentIndex(idx);
}

void VerifyExtraDialog::setExtras(QHash<int, Double> fes, QHash<int, Double> ses, QHash<int, Double> fms, QHash<int, Double> sms, QHash<int, int> fds, QHash<int, int> sds)
{
    this->fes = fes;
    this->ses = ses;
    this->fms = fms;
    this->sms = sms;
    this->fds = fds;
    this->sds = sds;
    showDatas();
}

void VerifyExtraDialog::init()
{
    QSqlQuery q;
        q.exec("select id,subName from FirSubjects where isView=1");
    while(q.next())
        ui->cmbSubs->addItem(q.value(1).toString(),q.value(0).toInt());
}

void VerifyExtraDialog::showDatas()
{
    ui->twFSub->clearContents();
    ui->twSSub->setRowCount(0);
    //1、显示一级科目余额
    QTableWidgetItem* item;
    ui->twFSub->setItem(0,CI_NAME,new QTableWidgetItem(ui->cmbSubs->currentText()));
    int key = fid * 10 + USD;
    int u_dir = fds.value(key);
    ui->twFSub->setItem(0,CI_USD_DIR,new QTableWidgetItem(QString::number(u_dir)));
    Double uv = fes.value(key);
    ui->twFSub->setItem(0,CI_USD_PV,new QTableWidgetItem(uv.toString2()));
    uv = fms.value(key);
    ui->twFSub->setItem(0,CI_USD_MV,new QTableWidgetItem(uv.toString2()));
    key = fid * 10 + RMB;
    int r_dir = fds.value(key);
    ui->twFSub->setItem(0,CI_RMB_DIR,new QTableWidgetItem(QString::number(r_dir)));
    Double rv = fes.value(key);
    ui->twFSub->setItem(0,CI_RMB_PV,new QTableWidgetItem(rv.toString2()));
    Double sum;
    int dir=DIR_P;
    if(u_dir == r_dir && (u_dir != 0 || r_dir != 0)){
        dir = u_dir;
        sum = uv + rv;
    }
    else if(u_dir + r_dir == 0 && (u_dir != 0 || r_dir != 0)){
        dir = r_dir;
        uv.changeSign();
        sum = uv + rv;
    }
    ui->twFSub->setItem(0,CI_SUM_DIR,new QTableWidgetItem(QString::number(dir)));
    ui->twFSub->setItem(0,CI_SUM,new QTableWidgetItem(sum.toString2()));
}

void VerifyExtraDialog::on_btnRead_clicked()
{
    fid = ui->cmbSubs->itemData(ui->cmbSubs->currentIndex()).toInt();
    if(fid == 0)
        return;
    y = ui->dateEdit->date().year();
    m = ui->dateEdit->date().month();
    //先读取保存在subjectExtra表中的一级科目余额，然后读取当前所选一级科目下的所有二级科目的余额
    BusiUtil::readExtraByMonth2(y,m,fes,fds,ses,sds);
    QHash<int,int>    fedr,sedr;             //期初余额方向（以本币计）
    BusiUtil::readExtraByMonth3(y,m,fms,fedr,sms,sedr);
    QHash<int,Double> fes_t,ses_t;
    bool exist = false;
    BusiUtil::readExtraByMonth4(y,m,fes_t,ses_t,exist);
    //用精确值代替直接从原币币转换来的外币值

    if(exist){
        QHashIterator<int,Double>* it = new QHashIterator<int,Double>(fes_t);
        while(it->hasNext()){
            it->next();
            fms[it->key()] = it->value();
        }
        it = new QHashIterator<int,Double>(ses_t);
        while(it->hasNext()){
            it->next();
            sms[it->key()] = it->value();
        }
    }
    showDatas();
}
